#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <exception>
#include <sstream>

#include "Sales_data.h"

using std::string;
using std::istream;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::cin;
using std::cout;
using std::endl;
using std::vector;


istream& printIs(istream& is)
{
	string word;
	while ( is >> word)
		cout << word << " ";
	return is;
}

int t8_1()
{
	printIs(cin);
	return 0;
}

int t8_4()
{
	ifstream f;
	try {
		f.open("/home/san/eclipse-workspace/CppPrimer/Sources/a.txt");
	} catch (std::runtime_error err) {
		std::cerr << "File or direction not exeits！！！";
	}

	// 使用if(f) 来查看文件是否正确打开
	if (f) {
		cout << "OK" << endl;
	}
	vector<string> lineVec = {};
	string buf;
	while (getline(f, buf))
		lineVec.push_back(buf);

	for (auto line: lineVec)
		cout << line << endl;
	return 0;
}

int t8_7()
{
	vector<Sales_data> v = {};
	ofstream out("/home/san/eclipse-workspace/CppPrimer/Sources/a.txt", ofstream::app);
	for (int i = 0; i < 5; ++i)
	{
		Sales_data sd(cin);
		out << sd.toString() << endl;
	}

	return 0;
}

int t8_10()
{
	ifstream in("/home/san/eclipse-workspace/CppPrimer/Sources/a.txt");
	vector<string> lineVec = {};
	string line, word;
	while (getline(in, line))
		lineVec.push_back(line);

	std::istringstream line_stream;
	for (auto l: lineVec)
	{
		// 清除流绑定
		line_stream.clear();
		line_stream.str(l);
		while (line_stream >> word)
			cout << word << " ";

		cout << endl;
	}
	return 0;
}



int main08()
{
//	t8_1();
//	t8_4();
//	t8_7();
	t8_10();
	return 0;
}
